/**
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.zbest.zpay.integration.aliyun;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;

/**
 * 
 *
 * @author czhou3306@gmail.com
 * @version $Id: AliyunOSSClientImpl.java, v0.1 2016年2月19日 上午9:54:43 czhou3306@gmail.com Exp $
 */
@Service("aliyunOSSClient")
public class AliyunOSSClientImpl implements AliyunOSSClient {

    private static final Logger logger          = LoggerFactory
                                                    .getLogger(AliyunOSSClientImpl.class);

    //private final static String IMAGEURL_PREFIX = "http://duodian.oss-cn-hangzhou.aliyuncs.com/";

    private final static String IMAGEURL_PREFIX = "http://cms.duodianyingxiao.com/";

    private String              endpoint        = "http://oss-cn-hangzhou.aliyuncs.com";
    private String              accessId        = "10RNQe2kblrV1VR2";
    private String              accessKey       = "M1pGSlA7rdLzJ6ZarD98jsvm2BPRGx";
    private String              bucketName      = "duodian";

    private OSSClient           client          = new OSSClient(endpoint, accessId, accessKey);

    /** 
     * @see com.zbest.zpay.integration.aliyun.AliyunOSSClient#uploadFile(java.lang.String, java.io.InputStream)
     */
    @Override
    public AliyunOSSResult uploadFile(String fileName, InputStream inputStream) {

        AliyunOSSResult aliyunOSSResult = new AliyunOSSResult();
        aliyunOSSResult.setAliyunUrl(IMAGEURL_PREFIX + fileName);
        PutObjectResult result = client.putObject(bucketName, fileName, inputStream);
        logger.info("文件上传结果result=" + result + ",fileName=" + fileName);

        return aliyunOSSResult;
    }

    /** 
     * @see com.zbest.zpay.integration.aliyun.AliyunOSSClient#deleteFile(java.lang.String)
     */
    @Override
    public void deleteFile(String fileName) {

        client.deleteObject(bucketName, fileName);
        logger.info("文件删除成功fileName=" + fileName);

    }

}
